import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-09-23
 * Time:21:04
 */
class MyArray1 {
    Object[] array = new Object[10];

    public void setValue(int pos,Object val) {
        array[pos] = val;
    }
    public Object getValue(int pos) {
        return array[pos];
    }
}

class MyArray<E /*extends Number*/> {
    Object[] array = new Object[10];

    public void setValue(int pos,E val) {
        array[pos] = val;
    }
    public E getValue(int pos) {
        return (E)array[pos];
    }
}
public class Test2 {
    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<>();//正常，因为 Integer 是 Number 的子类型
        System.out.println(myArray);

        MyArray<String> myArray1 = new MyArray<>();
        System.out.println(myArray1);//编译错误，因为 String 不是 Number 的子类型
    }
    public static void main3(String[] args) {
        Test2 test2 = new Test2();
        System.out.println(test2);
    }
    public static void main2(String[] args) {
        MyArray<Integer> myArray = new MyArray<Integer>();
        //MyArray<Integer> myArray = new MyArray<>();
        myArray.setValue(0,10);
        myArray.setValue(1,20);
        Integer a = myArray.getValue(1);//自动类型转换
        System.out.println(a);

        int val = myArray.getValue(0);//可以进行拆箱
        System.out.println(val);

        //myArray.setValue(3,"hello");//自动类型检查，如果不是Integer类就会编译报错
        System.out.println("========");
        MyArray<String> myArray1 = new MyArray<String>();
        //MyArray<String> myArray1 = new MyArray<>(); //前一个<>内要指定当前类型，后面一个<>里面一般不写
        myArray1.setValue(0,"hello");
        myArray1.setValue(1,"world");
        String s = myArray1.getValue(0);
        System.out.println(s);
    }
    public static void main1(String[] args) {
        MyArray1 myArray = new MyArray1();
        myArray.setValue(0,10);
        myArray.setValue(1,"hello");
        String str = (String) myArray.getValue(1);
        System.out.println(str);
    }
}
